<?php
/**
 * abstract classed used to define and give functionality to helper classes
 *
 * @package DefaultFramework
 * @version v2
 */

/**
 * 
 * 
 * @package System_Request
 * @subpackage System_Request_Helper
 * @author Mark Henderson
 */
abstract class System_Request_Helper_Abstract implements System_Request_Interface{
    /**
     * holds an instance of the response state object
     * 
     * @var object - instance of System_Request_Response_State
     * @access private
     */
    private $_response_state_object;
    
    /**
     * holds an instance of the error object
     * 
     * @var object - instance of System_Error_Base
     * @access protected
     */
    protected $_error_object;
    
    /**
     * Stores the message that will be going back to the user
     * 
     * @var array
     * @access protected
     */
    protected $_message = array();
    
    /**
     * property that hold the data for the given request
     * 
     * @var array
     * @access protected
     */
    private $_data = array();
    
    /**
     * property that holds all of the generated errors
     * 
     * @var array
     * @access protected
     */
    protected $_errors = array();
    
    /**
     * Public method used to process all of the action for a request
     * 
     * @access public
     * @return Object, instance of the request helper that is called
     */
    abstract public function process();
    
    /**
     * Adds a message to the message going back to the user.
     * 
     * @param String $key - the key for the return message
     * @param String $message - the return message
     * @access protected
     * @return Object, instance of the request helper that is called
     */
    protected function addMessage($key, $message){
        if ($message && $key) { 
            $this->_message[$key]   =  $message;
        }

        return $this;
    }
    
    /**
     * sets the message response status to the success code
     *
     * @access protected
     * @return Object, instance of the request helper that is called
     */
    protected function setSuccess(){
        $this->addMessage('status', $this->_response_state_object->get('success'));
        
        return $this;
    }
    
    /**
     * sets the message response status to the failure code
     *
     * @access protected
     * @return Object, instance of the request helper that is called
     */
    protected function setFailure(){
        $this->addMessage('status', $this->_response_state_object->get('failure'));
        
        return $this;
    }
    
    /**
     * sets the message response status to the error code
     *
     * @access protected
     * @return Object, instance of the request helper that is called
     */
    protected function setError(){
        $this->addMessage('status', $this->_response_state_object->get('error'));
        
        return $this;
    }
    
    /**
     * method used to set the data property
     *
     * @param Array $data 
     * @access protected
     * @return Object, instance of the request helper that is called
     */
    protected function setData(array $data = array()){
        $this->_data = $data;

        return $this;
    }

    /**
     * If the system run into an error, build the message stack to include the status as an error, and the error stack
     * 
     * @access protected
     * @return Object System_Request_Controller
     */
    protected function processErrors(){
        $this->setError();
        $errors = array();

        foreach ($this->_error_object->get() as $field => $message) {
            $errors[$field] = $message;
        }

        $this->addMessage('error_fields', $errors);

        return $this;
    }
    
    /**
     * returns the data property
     *
     * @access public
     * @return array
     */
    public function getData(){
        return $this->_data;
    }
    
    /**
     * returns the message stack
     *
     * @access public
     * @return array
     */
    public function getMessage(){
        return $this->_message;
    }
    
    /**
     * sets the current response state object
     *
     * @param Object $object - Instance of the response_state object
     * @access public
     * @return Object, instance of the request helper that is called
     */
    public function registerResposeStateObject($object){
        $this->_response_state_object = $object;
        
        return $this;
    }
    
    /**
     * sets the error object property
     *
     * @param Object $object - Instance of System_Error_Base
     * @access public
     * @return RETURN TYPE
     */
    public function registerErrorObject($object){
        $this->_error_object = $object;
        
        return $this;
    }
}